import Vue from 'vue'
import VueRouter from 'vue-router'
import store from "@/store"

Vue.use(VueRouter)

const routes = [
      {
        path:"/",
        component:() =>import("@/views/Layout"),
        redirect:"/home",
        children:[
          {
            path:"home",
            component:()=>import("@/views/Home")
          },
          {
            path:"user-info",
            component:()=>import("@/views/User/UserInfo.vue")
          },
          {
            path:"user-avatar",
            component:()=>import("@/views/User/UserAvatar.vue")
          },
          {
            path:"user-pwd",
            component:()=>import("@/views/User/UserPwd.vue")
          },
          {
            path:"art-cate",
            component:()=>import("@/views/Article/ArtCate.vue")
          },
          {
            path:"art-list",
            component:()=>import("@/views/Article/ArtList.vue")
          }
        ]
      },
      {
        path:"/reg",
        component:() => import("@/views/Register")
        //路由懒加载方式
        //路由懒加载指的是当页面跳转到/reg时，路由才加载对应组件
      },
      {
        path:"/login",
        component:() => import("@/views/Login")
      }
]

const router = new VueRouter({
  routes
})

const whiteList=["/reg","/login"]

router.beforeEach((to,from,next)=>{
    const token=store.state.token
    if(token){
      next()
    }else{
      if(whiteList.includes(to.path)){//前往登录与注册页则放行
        next()
      }else{
        next("/login") //强行跳转登录页
      }
    }
})


export default router
